package com.example.rabbitextensionsdemo.config;

import com.example.rabbitextensionsdemo.constant.Constants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class DXLConfig {
    // 声明正常队列和交换机
    @Bean("normalQueue")
    public Queue normalQueue() {
        return QueueBuilder.durable(Constants.NORMAL_QUEUE)
                .maxLength(10) // 队列最大值
                .ttl(10000)
                .deadLetterExchange(Constants.DLX_EXCHANGE) // 设置死信交换机
                .deadLetterRoutingKey("dlx") // 设置RoutingKey
                .build();
    }
    @Bean("normalExchange")
    public DirectExchange normalExchange() {
        return ExchangeBuilder.directExchange(Constants.NORMAL_EXCHANGE).build();
    }
    @Bean("normalBinding")
    public Binding normalBinding(@Qualifier("normalQueue") Queue queue, @Qualifier("normalExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("normal").noargs();
    }
    // 声明死信队列和交换机

    @Bean("dlxQueue")
    public Queue dlxQueue() {
        return QueueBuilder.durable(Constants.DLX_QUEUE).build();
    }
    @Bean("dlxExchange")
    public Exchange dlxExchange() {
        return ExchangeBuilder.directExchange(Constants.DLX_EXCHANGE).build();
    }
    @Bean("dlxBinding")
    public Binding dlxBinding(@Qualifier("dlxQueue") Queue queue, @Qualifier("dlxExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("dlx").noargs();
    }
}
